/******************倒计时开始***********************/
function checkTime(i){
    if (i < 10) {    
        i = "0" + i;    
    }    
    return i;    
}  
function timer(){
    var ts = (endTime - Math.floor(new Date().getTime() / 1000));//计算剩余的毫秒数
    var dd = parseInt(ts / 60 / 60 / 24, 10);//计算剩余的天数
    var hh = parseInt(ts / 60 / 60 % 24, 10);//计算剩余的小时数
    var mm = parseInt(ts / 60 % 60, 10);//计算剩余的分钟数
    var ss = parseInt(ts % 60, 10);//计算剩余的秒数
    dd = checkTime(dd);
    hh = checkTime(hh);
    mm = checkTime(mm);
    ss = checkTime(ss);
    document.getElementById("day").innerHTML = dd;
    document.getElementById("hour").innerHTML = hh;
    document.getElementById("min").innerHTML = mm;
    document.getElementById("sec").innerHTML = ss;
}
setInterval("timer()",1000);
/******************倒计时结束***********************/

$('#rules').on('click',function () {
    layer2.open({
      type: 1,
      shadeClose: true,
      content:'<div class="popmodal" id="rulespop"><div class="purplebox"><div class="rules"><button class="weui-btn obtn huobtn">活动规则</button><div class="scrolldiv"><h1>'+ activity.title +'</h1><p class="c-5">活动时间：'+ activity.start_time +'-' + activity.end_time + '</p>'+ activity.explain +'</div></div></div></div>',
      btn:'<i class="iconfont icon-guanbi closebtn"></i>'
    });
});

$('#prize').on('click',function () {
    var html = '';
    html += '<div class="popmodal" id="prizepop"><div class="purplebox"><div class="prize"><button class="weui-btn huobtn">活动奖品</button><ul>';
    for(var key in prize) {
        html += '<li><span>' + prize[key].rank_name + '</span><div><div class="f-left"><img src="'+ prize[key].poster +'" alt=""></div><p>'+ prize[key].prize +'</p>';
        if(prize[key].min_rank == prize[key].max_rank) {
            html += '<span>名次：' + prize[key].min_rank + '</span></div></li>';
        } else {
            html += '<span>名次：' + prize[key].min_rank + ' - ' + prize[key].max_rank + '</span></div></li>';
        }
    }
    html += '</ul></div></div></div>';
    layer2.open({
      type: 1,
      shadeClose: true,
      content:html,
      btn:'<i class="iconfont icon-guanbi closebtn2"></i>'
    });
});

$('.tijiao').on('click',function () {
    var url = $(this).parents('form').attr('action');
    var data = $(this).parents('form').serialize();
    $.ajax({
        type: "POST",
        url: url,
        data:  data,
        async: true,
        beforeSend: function () {
            jz = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
        },
        error: function (request) {
            layer.close(jz);
            layer.alert('网络错误',{icon:2}, function(index){
                layer.close(index);
            });
        },
        success: function (data) {
            //关闭加载层
            layer.close(jz);
            if (data.code == 1) {
                layer2.open({
                    type: 1,
                    shadeClose: true,
                    content:'<div class="popmodal" id="successpop"><div class="success"><div class="suctop"><div></div><div><span>'+ data.data.baby_name +'</span><img src="'+ data.data.avatar +'" alt=""><span>N0.'+ data.data.rank +'</span></div></div><div class="sucbot"> <div></div><div><p class="title">恭喜您！</p><h1>报名成功</h1><p>现在邀请您的好友为您投票，您还可以给为您投票的好友设置红包奖励哦！</p></div></div></div><button class="sharebtn layclose">分享好友拉票</button></div>',
                });
            }else if(data.code == -1){
                layer.msg(data.msg, {
                    icon: 2,
                    time: 5000
                }, function(){
                    window.location.href = '/index/index.html';
                });
            } else {
                layer.msg(data.msg, {
                    icon: 2,
                    time: 2000
                });
            }
        }
    });
});

$(document).on('change', '#applyForm .loadbtn', function (){
    var regex = /(.jpg|.jpeg|.png|.bmp)$/;
    var thisObj = $(this);
    var picture = $(this)[0].files[0];
    var which = thisObj.data('which');
    if (regex.test(picture.name.toLowerCase())) {
        var formdata = new FormData();
        formdata.append('picture', picture);
        $.ajax({
            type: "POST",
            url: "/File/upload",
            data: formdata,
            processData: false,
            contentType: false,
            dataType: "json",
            success: function (data) {
                if(data.code == 0) {
                    layer.msg(data.msg);
                } else {
                    var html = '<img src="'+ data.data.path + '" style="max-width: 100%">' +
                        '<input type="hidden" name="'+ which +'" value="'+ data.data.path + '"> '
                    thisObj.parents('.upimg').html(html);
                }
            }
        });
    } else {
        layer.msg("请上传图片");
        return false;
    }
});
